local Network = require"game/network"

local game = class("Game"){
    Network = nil;
    Utils = nil;
    scene = nil;
    scenes = {};
};

function game:initGame()
    self:initConst()
    self:initCallback()
    self:initNetwork()
    self:initScene()
end

function game:initConst()
    self.FONT_SIZE = 18;
    self.FONT = love.graphics.newFont("resource/font/default.otf",self.FONT_SIZE);
    self.FONT_WIDTH = self.FONT:getWidth("A");
    self.FONT_HEIGHT = self.FONT:getHeight();

    self.CHECKERBOARD_WIDTH = 4;
    self.CHECKERBOARD_HEIGHT = 4;
    self.CHECKERBOARD_NUMBER = self.CHECKERBOARD_WIDTH * self.CHECKERBOARD_HEIGHT;
    self.CHECKERBOARD_TEXT_MAX_NUMBER = 6;
    self.CHECKERBOARD_GRID_SIZE = self.FONT_SIZE * self.CHECKERBOARD_TEXT_MAX_NUMBER + 5;
    self.CHECKERBOARD_GRID_OFFSET = 10;
    self.CHECKERBOARD_TEXT_MAX_WIDTH = self.FONT_WIDTH * self.CHECKERBOARD_TEXT_MAX_NUMBER;
    self.CHECKERBOARD_TEXT_MAX_HEIGHT = self.FONT_HEIGHT

    self.RANDOM_NAME_TABLE = {}
    self.PLAYER_PORT_A = 0
    self.PLAYER_PORT_B = 1

    self.CALLBACK_TABLE = {
        "update",
        "keypressed",
        "keyreleased",
        "mousereleased",
        "mousepressed",
        "textinput",
        "draw",
        "quit",
    }

    self.CALLBACK_TABLE2 = {
        "_update",
        "_keypressed",
        "_keyreleased",
        "_mousereleased",
        "_mousepressed",
        "_textinput",
        "_draw",
        "_quit"
    }
    love.graphics.setFont(self.FONT);
    self.Utils = require"game/utils"
end

function game:initCallback()
    for i,callback_name in ipairs(self.CALLBACK_TABLE) do
        if self[callback_name] ~= nil then
            love[callback_name] = function(...)
                self[callback_name](self,...)
            end
        else
            love[callback_name] = function(...)
                if self.scene then
                    --self.CALLBACK_TABLE2[i]? 
                    --      CALLBACK_TABLE 与 CALLBACK_TABLE2是对应关系，所以直接所以得到是相同回调
                    self.scene[self.CALLBACK_TABLE2[i]](self.scene,...)
                end
            end
        end
    end
end

function game:initNetwork()
    self.Network = Network();
    self.Network:connectServer("127.0.0.1",23333)
end

function game:initScene()
    self:addScene("Start",require"game/scene/start"());
    self:addScene("Game",require"game/scene/game"());
    self:addScene("Room",require"game/scene/room"());
    self:addScene("CreateRoom",require"game/scene/create_room"());
    self:switchScene("Start")
end

function game:hasScene(name)
    return self.scenes[name] ~= nil
end

function game:addScene(name,scene)
    if scene == nil then
        scene = name;
        name = scene.name;
    end

    if self:hasScene(name) then return end
    self.scenes[name] = scene;
end

function game:switchScene(name)
    local scene = self.scenes[name];
    if scene ~= nil then
        if not scene._is_init then
            scene:_init();
        end
        if self.scene ~= nil then
            self.scene:_exit();
        end
        scene:_enter();
        self.scene = scene;
    end
end

function game:update(dt)
    self.Network.Sock:update();
    if self.scene then
        self.scene:_update(dt)
    end
end

function game:quit()
    self.Network.Sock:disconnectNow()
end

return game;